package particles;

import processing.core.*;
import java.util.Random;

public class Particle {
	private PVector loc; // sijainti
	private PVector vel; // nopeus
	private PVector acc; // kiihtyvyys
	private double alpha; // nopeusvektorin muuttumiskulma
	private float lifetime; // elinaika

	public Particle(PVector a, PVector v, PVector l, float lifetime,
			double angle) {
		this.acc = a.get();
		this.vel = v.get();
		this.loc = l.get();
		this.lifetime = lifetime;
		this.alpha = angle;
	}

	public void applyForce(PVector force) {
		this.acc.add(force);
	}

	public void setVelocity(PVector velocity) {
		this.vel.set(velocity);
	}

	public void setAcceleration(PVector acceleration) {
		this.acc.set(acceleration);
	}

	public PVector getLocation() {
		return this.loc.get();
	}

	public float getLifetime() {
		return this.lifetime;
	}

	public void update() {
		// only move when there is lifteime left
		if (this.lifetime > 0) {

			// update angle
			this.vel.set(new PVector(
					(float) (this.vel.x * Math.cos(alpha) - this.vel.y
							* Math.sin(alpha)), (float) (this.vel.x
							* Math.sin(alpha) + this.vel.y * Math.cos(alpha)),
					(float) 0));
			this.vel.add(this.acc);
			this.loc.add(this.vel);
			this.lifetime -= 1;
		}
	}

	public boolean isAlive() {
		return (this.lifetime > 0);
	}

}